package com.aabte.permissionframeworklearn.permission.controller;

import com.aabte.permissionframeworklearn.permission.beans.PageQuery;
import com.aabte.permissionframeworklearn.permission.beans.PageResult;
import com.aabte.permissionframeworklearn.permission.common.ResponseBean;
import com.aabte.permissionframeworklearn.permission.model.SysUser;
import com.aabte.permissionframeworklearn.permission.param.UserParam;
import com.aabte.permissionframeworklearn.permission.service.SysRoleService;
import com.aabte.permissionframeworklearn.permission.service.SysTreeService;
import com.aabte.permissionframeworklearn.permission.service.SysUserService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author Daniel
 */
@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysTreeService sysTreeService;
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/noAuth.page")
    public ModelAndView noAuth() {
        return new ModelAndView("noAuth");
    }

    @PostMapping("/save.json")
    @ResponseBody
    public ResponseBean saveUser(
            @RequestBody UserParam userParam
    ) {
        SysUser user = sysUserService.save(userParam);
        return ResponseBean.success(user);
    }

    @PutMapping("/update.json")
    @ResponseBody
    public ResponseBean updateUser(
            @RequestBody UserParam userParam
    ) {
        SysUser user = sysUserService.update(userParam);
        return ResponseBean.success(user);
    }

    @GetMapping("/page.json")
    @ResponseBody
    public ResponseBean pageUsersByDeptId(
            @RequestParam(value = "deptId") Long deptId,
            PageQuery pageQuery
    ) {
        PageResult<SysUser> pageResult = sysUserService.queryUsersByDeptId(deptId, pageQuery);
        return ResponseBean.success(pageResult);
    }

    @GetMapping("/acls.json")
    @ResponseBody
    public ResponseBean acls(
            @RequestParam(value = "userId") Long userId
    ) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("acls", sysTreeService.userAclTree(userId));
        map.put("roles", sysRoleService.getRoleListByUserId(userId));
        return ResponseBean.success(map);
    }


}
